<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    ***************************************************************************
    Updated: 13/05/2009
    
    TODO: 
    1. Since MonitoredDataItem could not appear if IsContextType="false", we need to add cases to all policy assertions xslt we made so far.
        
    Additional Info:
    1. there's no output for PassedInSOAPHeaders for example. since there's no corresponding MD-Additioanl Info in config file
    2. I found that for character like '<', '>', '"', etc, there're 2 sets of entity references (e.g. for '<', could be '&lt;' or '&#60;')
        check here for further info (http://en.wikipedia.org/wiki/Character_entity_reference)
        I think we shoud use xml standard which are: &lt;, &gt;, &amp;, &quot;, or &apos;, 
        but just for the example, we use &#x201d; for getting right result, in the future we can replace all &#201d; with &quod; and make a note for that
    3. WhenFor is not defined yet, since it's not a big issue, we will consider that later.
    4. A temporary root element added in output xml for well-formed.
    
    ****************************************************************************
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
    xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
    xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"
    xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om"
    xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"
    xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"
    xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"
    xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa"
    xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl"
    xmlns:tns="${currentFileURL}" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs" version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="input" select="document('../inputs/StockTradingGPA.xml')"/>
    <xsl:variable name="config" select="document('../inputs/config.xml')"/>

    <!-- define a newline variable for pretty output -->
    <xsl:variable name="newline">
        <xsl:text>&#10;</xsl:text>
    </xsl:variable>
    <!-- define the target namespace -->
    <xsl:variable name="tns">
        <xsl:text>tns:</xsl:text>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:element name="root">
            <xsl:comment>
                <xsl:text>This root element is for well-formed of output. Could be removed if we generate a corresponding output file with every Policy Assertion/Monitored Definition</xsl:text>
            </xsl:comment>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="$newline"/>
            <xsl:apply-templates select="$input/xmi:XMI"/>
        </xsl:element>
        <xsl:value-of select="$newline"/>
    </xsl:template>


    <xsl:template name="main" match="xmi:XMI">
        <!-- parameters recording AwU Expressions MASCID for further referencing -->
        <xsl:param name="TL-AWUER"/>
        <xsl:param name="CL-AWUER"/>
        <xsl:param name="TLT-AWUER"/>
        <xsl:param name="TLA-AWUER"/>

        <xsl:for-each select="$input/xmi:XMI/MASC_Profile:Meta_PA">
            <xsl:variable name="MPA_MASCID" select="./@MP-MASCID"/>
            <xsl:variable name="MPA_ManagementParty" select="./@MD-ManagementParty"/>
            <xsl:variable name="MPA_RuntimeMeasurementData" select="./@MP-RuntimeMeasurementData"/>
            <xsl:variable name="MPA_PartyForWhichUtilityIsExamined"
                select="./@PartyForWhichUtilityIsExamined"/>
            <xsl:variable name="MPA_RuntimeEventData" select="./@RuntimeEventData"/>
            <xsl:variable name="MPA_TangibilityBeforeAgreeability"
                select="./@TangibilityBeforeAgreeability"/>
            <xsl:variable name="MPA_UseAgreed" select="./@UseAgreed"/>
            <xsl:variable name="MPA_UseIntangibles" select="./@UseIntangibles"/>
            <xsl:variable name="MPA_UseNonagreed" select="./@UseNonagreed"/>
            <xsl:variable name="MPA_UseProbabilities" select="./@UseProbabilities"/>
            <xsl:variable name="MPA_UseTangibles" select="./@UseTangibles"/>
            <xsl:variable name="MPA_policyConflictResolutionStrategy"
                select="./@policyConflictResolutionStrategy"/>
            <xsl:variable name="MPA_ActionPolicyAssertions" select="./@ActionPolicyAssertions"/>
            <xsl:variable name="MPA_CommonCurrency" select="./@CommonCurrency"/>
            <!-- 4 AwU Expressions  -->
            <xsl:variable name="MPA_TimeLimit" select="replace(lower-case(./@TimeLimit), '\s+', '')"/>
            <xsl:variable name="MPA_CostLimit" select="replace(lower-case(./@CostLimit), '\s+', '')"/>
            <xsl:variable name="MPA_TiebreakLimitForAgreeability"
                select="replace(lower-case(./@TiebreakLimitForAgreeability), '\s+', '')"/>
            <xsl:variable name="MPA_TiebreakLimitForTangibility"
                select="replace(lower-case(./@TiebreakLimitForTangibility), '\s+', '')"/>

            <xsl:variable name="MPA_TimeLimit_original" select="normalize-space(./@TimeLimit)"/>
            <xsl:variable name="MPA_CostLimit_original" select="normalize-space(./@CostLimit)"/>
            <xsl:variable name="MPA_TiebreakLimitForAgreeability_original"
                select="normalize-space(./@TiebreakLimitForAgreeability)"/>
            <xsl:variable name="MPA_TiebreakLimitForTangibility_original"
                select="normalize-space(./@TiebreakLimitForTangibility)"/>

            <xsl:variable name="baseElement" select="./@base_Element"/>
            <xsl:variable name="SN">
                <!-- define sequence number by MPA's position -->
                <xsl:number value="position()" format="1"/>
            </xsl:variable>

            <xsl:for-each select="$input//packagedElement/node">
                <xsl:variable name="name" select="@name"/>
                <xsl:variable name="xmiID" select="@xmi:id"/>

                <xsl:choose>
                    <xsl:when test="$xmiID=$baseElement">

                        <!--
                            ***********************************************************

                                    varibales for monitoring definition constructs

                            ***********************************************************
                        -->

                        <!-- variables for AwU of TiebreakerLimitForTangibility -->
                        <xsl:variable name="AV_TLT"
                            select="substring-before(substring-after($MPA_TiebreakLimitForTangibility_original, 'AwUConst Value=&quot;'), '&quot;')"/>
                        <xsl:variable name="MDIN_TLT"
                            select="substring-before(substring-after($MPA_TiebreakLimitForTangibility_original, 'MonitoredData Name=&quot;'), '&quot;')"/>
                        <xsl:variable name="UNIT_TLT"
                            select="substring-before(substring-after($MPA_TiebreakLimitForTangibility_original, 'Unit=&quot;'), '&quot;')"/>
                        <xsl:variable name="OURI_TLT" select="substring-before($UNIT_TLT, ':')"/>
                        <xsl:variable name="T_TLT" select="substring-after($UNIT_TLT, ':')"/>

                        <!-- variables for AwU of TiebreakerLimitForAgreeability -->
                        <xsl:variable name="AV_TLA"
                            select="substring-before(substring-after($MPA_TiebreakLimitForAgreeability_original, 'AwUConst Value=&quot;'), '&quot;')"/>
                        <xsl:variable name="MDIN_TLA"
                            select="substring-before(substring-after($MPA_TiebreakLimitForAgreeability_original, 'MonitoredData Name=&quot;'), '&quot;')"/>
                        <xsl:variable name="UNIT_TLA"
                            select="substring-before(substring-after($MPA_TiebreakLimitForAgreeability_original, 'Unit=&quot;'), '&quot;')"/>
                        <xsl:variable name="OURI_TLA" select="substring-before($UNIT_TLA, ':')"/>
                        <xsl:variable name="T_TLA" select="substring-after($UNIT_TLA, ':')"/>
                        
                        <!-- variables for AwU of CostLimit -->
                        <xsl:variable name="AV_CL"
                            select="substring-before(substring-after($MPA_CostLimit_original, 'AwUConst Value=&quot;'), '&quot;')"/>
                        <xsl:variable name="MDIN_CL"
                            select="substring-before(substring-after($MPA_CostLimit_original, 'MonitoredData Name=&quot;'), '&quot;')"/>
                        <xsl:variable name="UNIT_CL"
                            select="substring-before(substring-after($MPA_CostLimit_original, 'Unit=&quot;'), '&quot;')"/>
                        <xsl:variable name="OURI_CL" select="substring-before($UNIT_CL, ':')"/>
                        <xsl:variable name="T_CL" select="substring-after($UNIT_CL, ':')"/>
                        
                        <!-- variables for AwU of TimeLimit -->
                        <xsl:variable name="AV_TL"
                            select="substring-before(substring-after($MPA_TimeLimit_original, 'AwUConst Value=&quot;'), '&quot;')"/>
                        <xsl:variable name="MDIN_TL"
                            select="substring-before(substring-after($MPA_TimeLimit_original, 'MonitoredData Name=&quot;'), '&quot;')"/>
                        <xsl:variable name="UNIT_TL"
                            select="substring-before(substring-after($MPA_TimeLimit_original, 'Unit=&quot;'), '&quot;')"/>
                        <xsl:variable name="OURI_TL" select="substring-before($UNIT_TL, ':')"/>
                        <xsl:variable name="T_TL" select="substring-after($UNIT_TL, ':')"/>
                        
                        <!-- variables for policyConflictStrategy -->
                        <xsl:variable name="CCS-URI">
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MetaPA-AdditionalInfo[@MASCID=$MPA_MASCID]/@CurrencyConversionService"
                            />
                        </xsl:variable>
                        <xsl:variable name="O-URI">
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MetaPA-AdditionalInfo[@MASCID=$MPA_MASCID]/@CurencyOntology"
                            />
                        </xsl:variable>
                        <xsl:variable name="OT">
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MetaPA-AdditionalInfo[@MASCID=$MPA_MASCID]/@OntologyType"
                            />
                        </xsl:variable>

                        <!-- variables for NameSpaces of MP-->
                        <xsl:variable name="MPA_TNS">
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/@UMLDiagramName"/>
                            <xsl:text>-MPA-WSP4MASC</xsl:text>
                        </xsl:variable>

                        <!-- the output templte has to be here, cannot use attributes set -->
                        <!--
                            ***********************************************************
                            
                            Define output for MPA
                            
                            ***********************************************************
                        -->
                        <xsl:comment>
                            <xsl:text>OUTPUT FOR MPA for UML Element: </xsl:text>
                            <xsl:value-of select="$name"/>
                            <xsl:value-of select="$newline"/>
                            <xsl:text> The MASCID is: </xsl:text>
                            <xsl:value-of select="$MPA_MASCID"/>
                        </xsl:comment>
                        <xsl:value-of select="$newline"/>
                        <xsl:element name="wsp:Policy">
                            <!-- constant namespaces -->
                            <xsl:namespace name="xs" select="'http://www.w3.org/2001/XMLSchema'"/>
                            <xsl:namespace name="wsp"
                                select="'http://schemas.xmlsoap.org/ws/2004/09/policy'"/>
                            <xsl:namespace name="masc-gp"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp'"/>
                            <xsl:namespace name="masc-se"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se'"/>
                            <xsl:namespace name="masc-ex"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex'"/>
                            <xsl:namespace name="masc-om"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om'"/>
                            <xsl:namespace name="masc-sc"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc'"/>
                            <xsl:namespace name="masc-ap"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap'"/>
                            <xsl:namespace name="masc-mp"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp'"/>
                            <xsl:namespace name="masc-up"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up'"/>
                            <xsl:namespace name="masc-config"
                                select="'http://www.nicta.com.au/projects/armature/masc-config'"/>
                            <xsl:namespace name="wsa"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa'"/>
                            <!-- schema locations -->
                            <xsl:namespace name="xsi"
                                select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                            <xsl:namespace name="schemaLocation"
                                select="'http://schemas.xmlsoap.org/ws/2004/09/policy 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-gp.xsd
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-se.xsd
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ex.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-sc.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-om.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ap.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-up.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-mp.xsd'"/>
                            <!-- namespaces for TargetNameSpace -->
                            <xsl:namespace name="targetNamespace" select="$MPA_TNS"/>
                            <xsl:namespace name="tns" select="$MPA_TNS"/>
                            <!-- namespaces for QoSOntology, MeasurementOntology and WSDLs -->
                            <xsl:namespace name="QoSOntology"
                                select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='qosontology']"/>
                            <xsl:namespace name="MeasurementOntology"
                                select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='measurementontology']"/>
                            <xsl:for-each
                                select="$config/masc-config:XMIFile/namespace::*[starts-with(normalize-space(lower-case(local-name())), 'wsdl')]">
                                <xsl:namespace name="{name()}">
                                    <xsl:value-of select="."/>
                                </xsl:namespace>
                            </xsl:for-each>
                            <xsl:value-of select="$newline"/>

                            <!-- AwUExp for TiebreakerLimitForTangibility -->
                            <xsl:comment>
                                <xsl:text>Definition of AwUExps for TiebreakerLimitForTangibility</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:choose>
                                <!-- AwU with operator -->
                                <xsl:when
                                    test="starts-with($MPA_TiebreakLimitForTangibility, '&lt;add') or starts-with($MPA_TiebreakLimitForTangibility, '&lt;multiply')">
                                    <xsl:if
                                        test="starts-with($MPA_TiebreakLimitForTangibility, '&lt;add')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TLT"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TLT"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'add'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TLT"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TLT"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                    <xsl:if
                                        test="starts-with($MPA_TiebreakLimitForTangibility, '&lt;multiply')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TLT"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TLT"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'multiply'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TLT"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TLT"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                </xsl:when>
                                <!-- AwU without Operator -->
                                <xsl:otherwise> 
                                    <xsl:call-template name="Arithmetic_With_Unit">
                                        <xsl:with-param name="AV" select="$AV_TLT"/>
                                        <xsl:with-param name="MDIN" select="$MDIN_TLT"/>
                                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                        <xsl:with-param name="OURI" select="$OURI_TLT"/>
                                        <xsl:with-param name="SN" select="$SN"/>
                                        <xsl:with-param name="T" select="$T_TLT"/>
                                    </xsl:call-template>           
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>
                            
                            <!-- AwUExp for TiebreakerLimitForAgreeability -->
                            <xsl:comment>
                                <xsl:text>Definition of AwUExps for TiebreakerLimitForAgreeability</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:choose>
                                <!-- AwU with operator -->
                                <xsl:when
                                    test="starts-with($MPA_TiebreakLimitForAgreeability, '&lt;add') or starts-with($MPA_TiebreakLimitForAgreeability, '&lt;multiply')">
                                    <xsl:if
                                        test="starts-with($MPA_TiebreakLimitForAgreeability, '&lt;add')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TLA"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TLA"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'add'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TLA"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TLA"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                    <xsl:if
                                        test="starts-with($MPA_TiebreakLimitForAgreeability, '&lt;multiply')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TLA"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TLA"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'multiply'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TLA"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TLA"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                </xsl:when>
                                <!-- AwU without Operator -->
                                <xsl:otherwise> 
                                    <xsl:call-template name="Arithmetic_With_Unit">
                                        <xsl:with-param name="AV" select="$AV_TLA"/>
                                        <xsl:with-param name="MDIN" select="$MDIN_TLA"/>
                                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                        <xsl:with-param name="OURI" select="$OURI_TLA"/>
                                        <xsl:with-param name="SN" select="$SN"/>
                                        <xsl:with-param name="T" select="$T_TLA"/>
                                    </xsl:call-template>           
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>
                            
                            <!-- AwUExp for CostLimit -->
                            <xsl:comment>
                                <xsl:text>Definition of AwUExps for CostLimit</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:choose>
                                <!-- AwU with operator -->
                                <xsl:when
                                    test="starts-with($MPA_CostLimit, '&lt;add') or starts-with($MPA_CostLimit, '&lt;multiply')">
                                    <xsl:if
                                        test="starts-with($MPA_CostLimit, '&lt;add')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_CL"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_CL"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'add'"/>
                                            <xsl:with-param name="OURI" select="$OURI_CL"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_CL"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                    <xsl:if
                                        test="starts-with($MPA_CostLimit, '&lt;multiply')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_CL"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_CL"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'multiply'"/>
                                            <xsl:with-param name="OURI" select="$OURI_CL"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_CL"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                </xsl:when>
                                <!-- AwU without Operator -->
                                <xsl:otherwise> 
                                    <xsl:call-template name="Arithmetic_With_Unit">
                                        <xsl:with-param name="AV" select="$AV_CL"/>
                                        <xsl:with-param name="MDIN" select="$MDIN_CL"/>
                                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                        <xsl:with-param name="OURI" select="$OURI_CL"/>
                                        <xsl:with-param name="SN" select="$SN"/>
                                        <xsl:with-param name="T" select="$T_CL"/>
                                    </xsl:call-template>           
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>
                            
                            <!-- AwUExp for TimeLimit -->
                            <xsl:comment>
                                <xsl:text>Definition of AwUExps for TimeLimit</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:choose>
                                <!-- AwU with operator -->
                                <xsl:when
                                    test="starts-with($MPA_TimeLimit, '&lt;add') or starts-with($MPA_TimeLimit, '&lt;multiply')">
                                    <xsl:if
                                        test="starts-with($MPA_TimeLimit, '&lt;add')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TL"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TL"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'add'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TL"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TL"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                    <xsl:if
                                        test="starts-with($MPA_CostLimit, '&lt;multiply')">
                                        <xsl:call-template name="AWU_with_Operator">
                                            <xsl:with-param name="AV" select="$AV_TL"/>
                                            <xsl:with-param name="MDIN" select="$MDIN_TL"/>
                                            <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                            <xsl:with-param name="O" select="'multiply'"/>
                                            <xsl:with-param name="OURI" select="$OURI_TL"/>
                                            <xsl:with-param name="SN" select="$SN"/>
                                            <xsl:with-param name="T" select="$T_TL"/>
                                        </xsl:call-template>
                                    </xsl:if>
                                </xsl:when>
                                <!-- AwU without Operator -->
                                <xsl:otherwise> 
                                    <xsl:call-template name="Arithmetic_With_Unit">
                                        <xsl:with-param name="AV" select="$AV_TL"/>
                                        <xsl:with-param name="MDIN" select="$MDIN_TL"/>
                                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                                        <xsl:with-param name="OURI" select="$OURI_TL"/>
                                        <xsl:with-param name="SN" select="$SN"/>
                                        <xsl:with-param name="T" select="$T_TL"/>
                                    </xsl:call-template>           
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- Definition of PolicuConflictResolutionStrategy -->
                            <xsl:comment>
                                <xsl:text>Definition of PolicuConflictResolutionStrategy</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-mp:PolicyConflictResolutionStrategy">
                                <xsl:attribute name="MASCID">
                                    <xsl:text>PCRS-</xsl:text>
                                    <xsl:value-of select="$MPA_MASCID"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$SN"/>
                                </xsl:attribute>
                                <xsl:attribute name="TangibilityBeforeAgreebility"
                                    select="$MPA_TangibilityBeforeAgreeability"/>
                                <xsl:attribute name="UseAgreed" select="$MPA_UseAgreed"/>
                                <xsl:attribute name="UseIntangible" select="$MPA_UseIntangibles"/>
                                <xsl:attribute name="UseNonAgreed" select="$MPA_UseNonagreed"/>
                                <xsl:attribute name="UseProbabilities"
                                    select="$MPA_UseProbabilities"/>
                                <xsl:attribute name="UseTangible" select="$MPA_UseTangibles"/>
                                <xsl:element name="masc-mp:CommonCurrency">
                                    <xsl:attribute name="CurrencyConversionService"
                                        select="$CCS-URI"/>
                                    <xsl:element name="masc-ex:Unit">
                                        <xsl:attribute name="Ontology" select="$O-URI"/>
                                        <xsl:attribute name="Type" select="$OT"/>
                                    </xsl:element>
                                </xsl:element>
                                <!-- references to awu expression -->
                                <xsl:element name="masc-mp:TimeLimit">
                                    <xsl:element name="masc-ex:ArithmeticWithUnitExpressionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$TL-AWUER"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                                <xsl:element name="masc-mp:CostLimit">
                                    <xsl:element name="masc-ex:ArithmeticWithUnitExpressionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$CL-AWUER"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                                <xsl:element name="masc-mp:TiebreakerLimitForTangibility">
                                    <xsl:element name="masc-ex:ArithmeticWithUnitExpressionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$TLT-AWUER"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                                <xsl:element name="masc-mp:TiebreakerLimitForAgreeability">
                                    <xsl:element name="masc-ex:ArithmeticWithUnitExpressionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$TLA-AWUER"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- Optional Schedule Definition -->
                            <!-- if has optional schedules definition -->
                            <xsl:if
                                test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules">
                                <xsl:comment>
                                    <xsl:text>Definition of Schedules </xsl:text>
                                    <xsl:value-of select="$name"/>
                                </xsl:comment>
                                <xsl:value-of select="$newline"/>
                                <xsl:copy-of
                                    select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules"/>
                                <xsl:value-of select="$newline"/>
                                <xsl:value-of select="$newline"/>
                            </xsl:if>

                            <!-- Definition of MPA -->
                            <xsl:comment>
                                <xsl:text>Definition of MPA</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-mp:MetaPolicyAssertion">
                                <xsl:attribute name="MASCID">
                                    <xsl:value-of select="$MPA_MASCID"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$SN"/>
                                </xsl:attribute>
                                <xsl:attribute name="ManagementParty" select="$MPA_ManagementParty"/>
                                <xsl:attribute name="PartyForWhichUtilityIsExamined"
                                    select="$MPA_PartyForWhichUtilityIsExamined"/>
                                <xsl:attribute name="IsActive"
                                    select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MetaPA-AdditionalInfo[@MASCID=$MPA_MASCID]/@IsActive"/>
                                <xsl:attribute name="Priority"
                                    select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MetaPA-AdditionalInfo[@MASCID=$MPA_MASCID]/@Priority"/>
                                <!-- make a APA reference for each APA inside ActionPolicyAssertions Tagged Value -->
                                <xsl:for-each
                                    select="tokenize(replace(substring-before(substring-after($MPA_ActionPolicyAssertions, '{'), '}'), '\s+', ''), ',')">
                                    <xsl:element name="masc-ap:ActionPolicyAssertionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="."/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:for-each>
                                <xsl:element name="masc-mp:PolicyConfilictResolutionStrategyRef">
                                    <xsl:attribute name="To">
                                        <xsl:value-of select="$tns"/>
                                        <xsl:value-of select="$MPA_PartyForWhichUtilityIsExamined"/>
                                    </xsl:attribute>
                                </xsl:element>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- TODO:could be deleted -->
                            <!-- boolean expressions constructs -->
                            <xsl:comment>
                                <xsl:text>Definition of optional BooleanExpressions for monitoring definition</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <!-- could add a for loop for multiple When-FilteringCondition -->
                            <xsl:choose>
                                <!-- if can not find MD Additional Info for selected stereotype -->
                                <xsl:when
                                    test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')][not(masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MPA_MASCID])]">
                                    <xsl:text>Can not find corresponding MD Additional Info in config file</xsl:text>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:apply-templates
                                        select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MPA_MASCID]/When-FilteringCondition"
                                    />
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                        </xsl:element>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>



    <!-- template for Arithmetic-with-unit constants -->
    <!-- e.g. <AwUConst Value="Variable1" Unit="Namespace:Variable2"/> -->
    <xsl:template name="Arithmetic_With_Unit">
        <xsl:param name="MPA_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="AV"/>
        <xsl:param name="OURI"/>
        <xsl:param name="T"/>
        <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>AUE-MP-</xsl:text>
                <xsl:value-of select="$MPA_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$MDIN"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:ArithmeticWithUnitConstant">
                <xsl:element name="masc-ex:ArithmeticValue">
                    <xsl:attribute name="Value" select="$AV"/>
                </xsl:element>
                <xsl:element name="masc-ex:Unit">
                    <xsl:attribute name="Ontology" select="$OURI"/>
                    <xsl:attribute name="Type" select="$T"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- template for arithmetic-without-unit -->
    <!-- e.g. <AwUConst Value="Variable1"/> -->
    <xsl:template name="Arithmetic_Constants_Without_Unit">
        <xsl:param name="MPA_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="CV"/>
        <xsl:element name="masc-ex:ArithmeticExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>MD-AC-</xsl:text>
                <xsl:value-of select="$MPA_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$MDIN"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:FloatConstant">
                <xsl:attribute name="Value" select="$CV"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- template for arithmetic-with-unit with operators -->
    <xsl:template name="AWU_with_Operator">
        <xsl:param name="MPA_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="O"/>
        <xsl:param name="AV"/>
        <xsl:param name="OURI"/>
        <xsl:param name="T"/>
        <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>AUE-MP-</xsl:text>
                <xsl:value-of select="$MPA_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:BinaryArithmeticWithUnitOperator">
                <xsl:attribute name="Type">
                    <xsl:choose>
                        <xsl:when test="lower-case(string($O))='add'">
                            <xsl:text>+</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($O))='multiply'">
                            <xsl:text>*</xsl:text>
                        </xsl:when>
                    </xsl:choose>
                </xsl:attribute>
            </xsl:element>
            <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
                <xsl:attribute name="MASCID">
                    <xsl:text>AUE-MP-</xsl:text>
                    <xsl:value-of select="$MPA_MASCID"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$MDIN"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$SN"/>
                </xsl:attribute>
                <xsl:element name="masc-gp:MonitoredDataItemRef">
                    <xsl:attribute name="To">
                        <xsl:value-of select="$tns"/>
                        <xsl:value-of select="$MDIN"/>
                    </xsl:attribute>
                </xsl:element>
            </xsl:element>
            <xsl:choose>
                <!-- sub-case1. for Add Operater, the Arithmetic Expression must has Unit -->
                <xsl:when test="string($O)='Add'">
                    <xsl:call-template name="Arithmetic_With_Unit">
                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                        <xsl:with-param name="MDIN" select="$MDIN"/>
                        <xsl:with-param name="SN" select="$SN"/>
                        <xsl:with-param name="AV" select="$AV"/>
                        <xsl:with-param name="OURI" select="$OURI"/>
                        <xsl:with-param name="T" select="$T"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- sub-case2.for Multiply Operater, the Arithmetic Expression must has NO Unit -->
                <xsl:when test="string($O)='Multiply'">
                    <xsl:call-template name="Arithmetic_Constants_Without_Unit">
                        <xsl:with-param name="SN" select="$SN"/>
                        <xsl:with-param name="MDIN" select="$MDIN"/>
                        <xsl:with-param name="MPA_MASCID" select="$MPA_MASCID"/>
                        <xsl:with-param name="CV" select="$AV"/>
                    </xsl:call-template>
                </xsl:when>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
